//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.util
{
	import flash.desktop.DockIcon;
	import flash.desktop.NativeApplication;
	import flash.desktop.NotificationType;
	import flash.desktop.SystemTrayIcon;
	import flash.display.NativeMenu;
	import flash.display.NativeWindow;
	import flash.events.InvokeEvent;
	import flash.events.ScreenMouseEvent;

	/**
	 * 托盘停靠 辅助类
	 * 
	 * <pre>
	 *  var dock:DockUtil = new DockUtil(nativeWindow);
	 *  dock.icon16 = icon16;
	 *  dock.icon32 = icon32;
	 * 	dock.icon48 = icon48;
	 * 	dock.icon128 = icon128;
	 * 	dock.men = NativeMenu;
	 *  dock.dock();    // 开始停靠
	 *  dock.undock();  // 取消停靠
	 *  dock.notify();  // 提醒
	 * </pre>
	 * 
	 * @author rogo
	 */	
	public class DockUtil
	{
		public function DockUtil(window:NativeWindow)
		{
			dockWindow = window;
			
			initDock();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		public var icon16:Class;
		public var icon32:Class;
		public var icon48:Class;
		public var icon128:Class;
		
		private var dockWindow:NativeWindow;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		private function initDock():void
		{
			// Mac OS
			if(NativeApplication.supportsDockIcon)
				NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, window_invokeHandler);
			
			// Windows OS
			if(NativeApplication.supportsSystemTrayIcon)
			{
				var trayIcon:SystemTrayIcon = SystemTrayIcon(NativeApplication.nativeApplication.icon);
				trayIcon.addEventListener(ScreenMouseEvent.CLICK, trayIcon_clickHandler);
			}
		}
		
		public function set menu(value:NativeMenu):void
		{
			// Mac OS
			if(NativeApplication.supportsDockIcon)
				DockIcon(NativeApplication.nativeApplication.icon).menu = value;
			
			// Windows OS
			if(NativeApplication.supportsSystemTrayIcon)
				SystemTrayIcon(NativeApplication.nativeApplication.icon).menu = value;
		}
		
		public function set tooltip(value:String):void
		{
			// tooltip only support in Window OS
			if(NativeApplication.supportsSystemTrayIcon)
				SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = value;
		}

		
		/**
		 * 开始托盘停靠
		 */		
		public function dock():void
		{
			if (!dockWindow)
				return;
			
			var bitmaps:Array = [];
			if (icon16)
				bitmaps.push(new icon16);
			if (icon32)
				bitmaps.push(new icon32);
			if (icon48)
				bitmaps.push(new icon48);
			if (icon128)
				bitmaps.push(new icon128);
			
			dockWindow.visible = false;
			NativeApplication.nativeApplication.icon.bitmaps = bitmaps;
			dockWindow.minimize();
		}
		
		/**
		 * 停止托盘停靠
		 */		
		public function undock():void
		{
			if (!dockWindow)
				return;
			
			dockWindow.visible = true;
			NativeApplication.nativeApplication.icon.bitmaps = [];
			dockWindow.activate();
		}
		
		/**
		 * 通知 Mac OS 将以跳动的方式提醒
		 */		
		public function notify():void
		{
			// Mac OS
			if(NativeApplication.supportsDockIcon)
				DockIcon(NativeApplication.nativeApplication.icon).bounce();
			
			// Windows OS
			if(NativeApplication.supportsSystemTrayIcon)
				dockWindow.notifyUser(NotificationType.INFORMATIONAL);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function window_invokeHandler(e:InvokeEvent):void
		{
			undock();
		}
		
		private function trayIcon_clickHandler(e:ScreenMouseEvent):void
		{
			undock();
		}
	}
}